const koa = require('koa');
const Router = require('koa-Router');
const bodyparser = require('koa-bodyparser');
const { koaBody } = require('koa-body');
const cors = require('@koa/cors');
const app = new koa();
const router = new Router();
const {AlipaySdk} = require('alipay-sdk');
// esmodule / typescript，可以使用 import AlipaySdk from 'alipay-sdk';
// 普通公钥模式
const alipaySdk = new AlipaySdk({
    appId: '9021000137683706',
    // keyType: 'PKCS1', // 默认值。请与生成的密钥格式保持一致，参考平台配置一节
    privateKey: 'MIIEpAIBAAKCAQEAodik43NPBGTitKjK64zIKWhPIYG8BY++NqDbaz9Lm1HnFntIPuikGCZAl26yzrpCXPQTRBTMDasx2MSCSafV1SitXcAAY+A+12yWD6pj3zbNFZvosNz9sVHTS8NTK/BN6YtF6cUHKHZOe/dB0oiHxCCjGhxrtVK4HRTGBsbpTbhtoT0morWYoiEUAXHQK/W5nnk/MRyJjueCbS3YvMr5H0lUEf6d8OK62SX148gBGUA4yOffZQWnTYycV4yo8oR5OZlRO/BRw+DHt857h+m4dZdHuImicoIsAIbIQj0nc19PBIdi7d+YZcKE/g/vyk196tLVDqM9kAr0sTfFZY+1cQIDAQABAoIBAAp7ERJ5oSBTuCqi06E1WLD1xEoTMK3bhSHcgNkNHEROpXxbN257ekTmFUC5NnMUtL94Ptcl0aEA+RVkhFtotvw6yh9ME7pkBy3EJmOVUSpfTiCcHBF3/iXBesFpQLWTtjftgPWGgNQp9SMp++U6WjBlPKqwNHllhe68Laa6stkJgXrlNoiMKr+SLbgckevUg+I1Ju6cIPZO0ugTNf4e1hjL4lq939OJSogSCI/yZWbwtq7YarYD0wFyrBmuVfnQe+ly2PtjhBWQoxZ+jcYSQPySk1dNYd1dqp7us05M8DTGYrGOf8zpHQAxJD9gkLnjPFbRpVUU7LAYmiipQmU3GsECgYEA+5g/op6WA9pJ7B4bo4OPgNSVkxySMK9GmLUuPainkKkAvg+KCkRLogL3Nv+hnUmhQYcshNECZGAPRdjG0NwJ0onUvGgPlvRVBsK+1rjNyeAyTS8EgVlj8Yx4Du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t857h+m4dZdHuImicoIsAIbIQj0nc19PBIdi7d+YZcKE/g/vyk196tLVDqM9kAr0sTfFZY+1cQIDAQAB',
    gateway:'https://openapi-sandbox.dl.alipaydev.com/gateway.do',
});
app.use(bodyparser());
app.use(koaBody({ multipart: true }));
app.use(cors());
app.use(router);
router.get('/api/pay', async (ctx) => {
    console.log(ctx.query);
    let { id, title, price } = ctx.query;
    const bizContent = {
  out_trade_no: id,
  product_code: "FAST_INSTANT_TRADE_PAY",
  subject: title,
  body: "234",
  total_amount: price
};

// 支付页面接口，返回 html 代码片段，内容为 Form 表单
const result = alipaySdk.pageExec('alipay.trade.page.pay', {
  method: 'GET',
  bizContent,

});
    ctx.body = {
        code: 200,
        msg: '支付成功',
        result:result
    }
    console.log(result);
})
app.listen(9001, () => {
    console.log('服务器启动成功  端口号9001');
})